package com.yc.UI;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.dao.TrainDao;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.widgets.Combo;

/**
 *票务面板
 * @author UNO
 * 2021年6月24日下午7:08:16
 */
public class TicketingUI extends Composite {
	private Table table;
	public static String date = null;  //用户选择的出发日期
	public String cname;  //用户选中的哪一趟车的名字
	public static Map<String, Object> amap; //存放用户选中的车的信息
	public String str = null;  //选择性加0 之后的时间
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public TicketingUI(Composite parent, int style) {

		super(parent, style);

		setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(this, SWT.NONE);

		sashForm.setOrientation(SWT.VERTICAL);

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		Label label = new Label(composite_1, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label.setBounds(10, 28, 86, 35);
		label.setText("出发站");

		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setText("终点站");
		label_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_1.setBounds(246, 28, 86, 35);

		//查询按钮
		Label label_1_1 = new Label(composite_1, SWT.NONE);
		label_1_1.setText("查询");
		label_1_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 14, SWT.NORMAL));
		label_1_1.setBounds(709, 28, 58, 35);

		DateTime dateTime = new DateTime(composite_1, SWT.BORDER);
		dateTime.setBounds(495, 28, 159, 35);
		
		Combo combo = new Combo(composite_1, SWT.NONE);
		combo.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		combo.setBounds(118, 28, 98, 35);
		combo.setItems(new String[] {"衡阳", "长沙"});
		
		Combo combo_1 = new Combo(composite_1, SWT.NONE);
		combo_1.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
		combo_1.setBounds(353, 28, 98, 35);
		combo_1.setItems(new String[] {"衡阳", "长沙", "广州", "北京"});
		
		Composite composite = new Composite(sashForm, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_TRANSPARENT));

		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		//图片自适应
		//		SWTUtil.imagesSize(composite);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));

		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn_1 = new TableColumn(table, SWT.CENTER);
		tableColumn_1.setWidth(85);
		tableColumn_1.setText("列车序号");

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn.setWidth(150);
		tblclmnNewColumn.setText("列车类型");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_1.setWidth(85);
		tblclmnNewColumn_1.setText("列车名");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.CENTER);
		tableColumn_2.setWidth(80);
		tableColumn_2.setText("列车状态");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("出发站");

		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("终点站");

		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.CENTER);
		tblclmnNewColumn_4.setWidth(100);
		tblclmnNewColumn_4.setText("出发时间");

		TableColumn tableColumn = new TableColumn(table, SWT.CENTER);
		tableColumn.setWidth(100);
		tableColumn.setText("到站时间");
		
		//表格游标
		TableCursor tableCursor = new TableCursor(table, SWT.NONE);
		
		sashForm.setWeights(new int[] {85, 510});


		//栅栏式大小高度固定
		sashForm.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				sashForm.setWeights(new int[] {85, sashForm.getSize().y - 85});
			}
		});
	
		//表格游标，点击进入详细购票界面
		tableCursor.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				TableItem ti = tableCursor.getRow();//获取用户右击选中的行
				TrainDao trainDao = new TrainDao();
							
				String year = String.valueOf(dateTime.getYear());
				String month = String.valueOf(dateTime.getMonth() + 1);
				String day = String.valueOf(dateTime.getDay());
				if(month.length() < 2) {
					month = "0" + month;
				}
				if(day.length() < 2) {
					day = "0" + day;
				}
				date = year +"/" + month + "/" + day; //用户选择的出发日期
				
				cname = ti.getText(2); //获取第三列的值，即列车名
				//查找用户点击的那一趟列车的基本信息
				amap = trainDao.find(cname);
				
				str = amap.get("c_starttime").toString();
				if( amap.get("c_starttime").toString().length() < 5) {//8:45
					str = "0" + amap.get("c_starttime").toString();
				}

				String starttime = date + " " + str;//发车时间
				System.out.println(StringUtil.getTimes());
				System.out.println(starttime);
				
				int i = StringUtil.getTimes().compareTo(starttime);
				if(i > 0) {
					SWTUtil.showMessage(getShell(), "提示", "日期无效...");				
					return;
				}
				
				//查查看有没有那一天的票务表，如果没有，就生成那一天的那趟车的票务表
				if(trainDao.findPrice(cname, date) == null) {
					//生成用户点击的那一趟列车的票务表
					trainDao.GeneratePrice(cname, date);
				}
												
				BuyingTicket bt = new BuyingTicket();
				bt.open();
			}
		});

		//先展示所有列车
		showAllTrain();
		
		//点击查询生成查询信息
		label_1_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				TrainDao trainDao = new TrainDao();
				String startstation = combo.getText().trim();
				String destination = combo_1.getText().trim();
				if(startstation == null && destination == null) {
					return;
				}
				List<Map<String, String>> list = trainDao.findUsefulTrain(startstation, destination);//查询适合列车的基本信息
				table.removeAll(); //移除表格中原有的数据
				if (list == null || list.isEmpty()) {
					SWTUtil.showMessage(getShell(), "提示", "暂无相关站点");
					return;
				}
				TableItem ti = null; //行对象
				for(Map<String, String> map : list) {
					ti = new TableItem(table, SWT.NONE);
					ti.setData("c_status", map.get("c_status"));
					if("0".equals(map.get("c_status"))) {
						ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"正常", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
					}else {
						ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
						ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"停运", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
					}

				}
			}
		});

	}


	/**
	 * 从数据库查询到所有列车，并显示
	 */
	public void showAllTrain() {
		TrainDao trainDao = new TrainDao();
		List<Map<String, String>> list = trainDao.findAllTrain();//查询所有列车的基本信息
		//将所有列车显示到表格中
		table.removeAll(); //移除表格中原有的数据
		if (list == null || list.isEmpty()) {
			return;
		}
		TableItem ti = null; //行对象
		for(Map<String, String> map : list) {
			ti = new TableItem(table, SWT.NONE);
			ti.setData("c_status", map.get("c_status"));
			if("0".equals(map.get("c_status"))) {
				ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"正常", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
			}else {
				ti.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
				ti.setText(new String[] {map.get("c_id"), map.get("c_type"),map.get("c_name"),"停运", map.get("c_startstation"),map.get("c_destination"),map.get("c_starttime"),map.get("c_endtime")});
			}

		}

	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
